$(function () {
    refashTable();
    // setInterval(
    //     refashTable
    //     ,1000*60);
});
function refashTable(){

    $("#jqGrid").jqGrid('clearGridData');
    $("#jqGrid").jqGrid({
        url: baseURL + 'guide/applay',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'id', hidden: true, index: "id",  key: true },
            { label: '编号', name: 'sign', width: 80 },
            { label: '姓名', name: 'sname', width: 80 },
            { label: '班级', name: 'bname',  width: 40 },
            { label: '类型', name: 'name',  width: 80 },
            { label: '佐证材料', name: 'img',width: 85,formatter:function(value,options,rowData){

                    if(value!=null && value.indexOf(",")>-1){
                        var imgs =value.split(",");
                        var igs="";
                        for (var i= 0;i<imgs.length;i++){
                             igs =igs+  '<a class="view"  href="javascript:void(0)" >' +
                                '<img onclick="clickimg(\''+imgs[i]+'\')" style="width:300;height:40px;"  src="'+baseUrl+'/'+imgs[i]+'" />' +
                                '</a>'
                        }
                        return igs;
                    }else if(value!=null && value.indexOf(",")==-1){
                        var s = '<a class="view"  href="javascript:void(0)" >' +
                            '<img onclick="clickimg(\''+value+'\')" style="width:300;height:40px;"  src="'+baseUrl+'/'+value+'" />' +
                            '</a>';
                        return s;
                    }else{
                        return '';
                    }

                }
            },
            { label: '时间', name: 'time',  width: 80 },
            { label: '状态', name: 'status', width: 80  ,formatter:function(value,options,rowData){
                    if(value == '0'){
                        return '<b><span  onclick="update('+rowData.id+')"  style="cursor:pointer;" class="label label-success">审批</span></b>';
                    }else if(value == '1'){
                        return '已通过';
                    }else if(value == '2'){
                        return '未通过';
                    }
                }
            }

        ],
        viewrecords: true,

        autowidth:true,
        multiselect: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    }).trigger("reloadGrid");
};
function clickimg(e){

    var url = baseUrl + '/' +e;

    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        area: ['50%', '50%'],
        skin: 'layui-layer-nobg', //没有背景色
        shadeClose: true,
        content: '<img style="width: 100%;height: 100%" src="'+url+'"/>'
    });
}
Vue.component('v-select', VueSelect.VueSelect);
var vm = new Vue({
    el:'#rrapp',
    data:{
        showDiv: null,
        showList: true,
        showPDiv: false,
        title:null,
        stu:{
            sname:null,
            bname:null,
            atypeName:null,
            btypeName:null,
            score:null,
            status:null,
            beizhu:null

        }

    },
    created: function () {

    },
    methods: {
        query: function (e) {

            vm.reload(e);
        },
        choose: function(){
            if(vm.stu.status == 2){
                vm.title = '原因',
                vm.showPDiv = true,
                vm.showDiv = true
            }else{
                vm.title = '分值',
                vm.showPDiv = true,
                vm.showDiv = false
            }

        },
        dels: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "hq/repair/delete/0",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        zcsave:function(){
            vm.stu
            // $.get(baseURL + "guide/applaySave/",{"id":id}, function(r){
            //
            // });
            $.ajax({
                type: "POST",
                url: baseURL +  "guide/applaySave",
                contentType: "application/json",
                data: JSON.stringify(vm.stu),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function (e) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');

            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'status': e},
                page:page
            }).trigger("reloadGrid");
        }
    }
});

function update(id) {
    vm.showList = false;

    $.get(baseURL + "guide/applayOne/",{"id":id}, function(r){
        vm.stu = r.apply;
        var imgs = createImg(r.apply.img)
        $("#imgs").empty();
        $("#imgs").append(imgs);
    });

}

function createImg(value){
    if(value!=null && value.indexOf(",")>-1){
        var imgs =value.split(",");
        var igs="";
        for (var i= 0;i<imgs.length;i++){
            //  igs =igs+"<img  style='max-width: 50%; max-height: 50%' src='"+baseUrl+"/"+imgs[i]+"' /><br>";
            igs =igs+  '<a class="view"  href="javascript:void(0)" >' +
                '<img onclick="clickimg(\''+imgs[i]+'\')" style="width:300;height:40px;"  src="'+baseUrl+'/'+imgs[i]+'" />' +
                '</a>'
        }
        return igs;
    }else if(value!=null && value.indexOf(",")==-1){
        var s = '<a class="view"  href="javascript:void(0)" >' +
            '<img onclick="clickimg(\''+value+'\')" style="width:300;height:40px;"  src="'+baseUrl+'/'+value+'" />' +
            '</a>';
        return s;
    }else{
        return '';
    }
}
var baseUrl= baseUrl();
function baseUrl()
{
    //获取当前网址，如： http://localhost:8080/myproj/view/my.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： myprojiew/my.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/myproj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    //得到了 http://localhost:8080/myproj
    var realPath=localhostPaht+projectName;
    return realPath;
    //alert(realPath);
}